/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.server.main;

/**
 *
 * @author Administrator
 */

import cn.ac.iie.di.ban.ycl.wx.video.server.common.RuntimeEnv;
import cn.ac.iie.di.ban.ycl.wx.video.server.config.Configuration;
import cn.ac.iie.di.ban.ycl.wx.video.server.handler.VideoHandler;
import cn.ac.iie.di.ban.ycl.wx.video.server.handler.WriteJsonHandler;
import cn.ac.iie.di.ban.ycl.wx.video.server.timerTask.MapTask;
import static cn.ac.iie.di.ban.ycl.wx.video.server.utils.Suffixutil.mkdirs;
import cn.ac.iie.di.commons.httpserver.framework.server.HttpServer;
import java.io.File;
import java.util.Timer;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class VideoServer {

    static HttpServer httpServer = null;
    static Logger logger = Logger.getLogger(VideoServer.class.getName());

    public static void main(String[] args) {
//        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
//        logger.info("log4j.properties has been configured");
        try {
            init();
            mkDir();
            Timer timer = new Timer();
            MapTask mapTask = new MapTask();
            timer.schedule(mapTask, 1000, 500);
            for (int i = 0; i < Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.writeJsonThreadSize)); i++) {
                WriteJsonHandler writeJsonThread = new WriteJsonHandler(i);
                Thread thread = new Thread(writeJsonThread);
                thread.setName("writeJosnThread_" + i);
                thread.start();
            }
            startup();
            logger.info("server has been started .");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "config.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void startup() {
        try {
            httpServer = HttpServer.getHttpServer(
                    (String) RuntimeEnv.getParam(RuntimeEnv.JETTY_SERVER),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_PORT)),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_THREADNUM)));
            httpServer.registerContext("/resources");
            httpServer.registerContextHandler("/resources", "/WxVideoUrl", VideoHandler.class);//阅读数
           
            httpServer.startup();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    public static void mkDir() {
        logger.info("mkdir " + (String) RuntimeEnv.getParam(RuntimeEnv.loongstore) + " = " + mkdirs
        
        
        ((String) RuntimeEnv.getParam(RuntimeEnv.loongstore)));
        logger.info("mkdir " + (String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath) + " = " + mkdirs((String) RuntimeEnv.getParam(RuntimeEnv.catchFilePath)));
        logger.info("mkdir " + (String) RuntimeEnv.getParam(RuntimeEnv.requestDir) + " = " + mkdirs((String) RuntimeEnv.getParam(RuntimeEnv.requestDir)));
        logger.info("mkdir " + (String) RuntimeEnv.getParam(RuntimeEnv.responseDir) + " = " + mkdirs((String) RuntimeEnv.getParam(RuntimeEnv.responseDir)));
    }

}
